package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.admin.password.UpdatePasswordBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.user.Dashboard;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;

import com.cskaoyan.mapper.MarketAdminMapper;

import com.cskaoyan.mapper.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.apache.shiro.subject.Subject;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author stone
 * @date 2022/09/06 11:44
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper adminMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketGoodsProductMapper productMapper;

    @Override
    public MarketAdmin queryById(Integer id) {
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(id);
        return marketAdmin;
    }

    @Override
    public void modify(MarketAdmin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public List<MarketAdmin> query(String username, String password, String[] avatars,
                                   Integer page, Integer limit) {
        // 执行查询之前 → 使用PageHelper开启分页就可以了
        PageHelper.startPage(page, limit);
        MarketAdminExample example = new MarketAdminExample();
        example.setDistinct(true);
        //example.setOrderByClause("username asc");
        // List<Criterion> 向这个列表里塞入数据
        // 条件查询

        MarketAdminExample.Criteria criteria = example.createCriteria();//这个实例里封装了Criterion的列表
        // 使用Example中的Criteria提供的方法，就可以构造Criterion实例，并且呢添加到list中
        /*criteria.andUsernameEqualTo(username)
                .andPasswordLike("%" + password + "%")
                .andAvatarIn(Arrays.asList(avatars));*/
        if (username != null && !"".equals(username)) {//StringUtils isEmpty
            criteria.andUsernameEqualTo(username);
        }
        if (password != null && !"".equals(password)) {

            criteria.andPasswordLike("%" + password + "%");
        }
        if (avatars != null && avatars.length > 0) {

            criteria.andAvatarIn(Arrays.asList(avatars));
        }
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        // page limit total
        // 把mybatis查询出来的原始结果放进去
        PageInfo pageInfo = new PageInfo(marketAdmins);
        long total = pageInfo.getTotal();
        return marketAdmins;
    }

    // 首页仪表盘查询实现
    @Override
    public Dashboard queryDashboard() {
        Dashboard dashboard = new Dashboard();
        Integer goodsTotal = goodsMapper.queryGoodsTotal();
        Integer userTotal = userMapper.queryUserTotal();
        Integer productTotal = productMapper.queryProductTotal();
        Integer orderTotal = orderMapper.queryOrderTotal();

        // 封装查询结果
        dashboard.setGoodsTotal(goodsTotal);
        dashboard.setUserTotal(userTotal);
        dashboard.setProductTotal(productTotal);
        dashboard.setOrderTotal(orderTotal);
        return dashboard;
    }

    // 修改管理员密码
    @Override
    public void updatePassword(UpdatePasswordBO password) {
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin admin = (MarketAdmin) subject.getPrincipal();
        Integer id = admin.getId();
        if (password.getNewPassword().equals(password.getNewPassword2())) {
            String newPassword = password.getNewPassword();
            adminMapper.updateById(newPassword,id);
        }
    }

    /**
     * @author zjq
     * 根据姓名模糊查询管理员
     * @date 2022/11/19 17:13
     */
    @Override
    public CommonData fuzzyQuery(String username, Integer page, Integer limit) {

        PageHelper.startPage(page, limit);
        MarketAdminExample example = new MarketAdminExample();
        //
//        example.setDistinct(true);

        MarketAdminExample.Criteria criteria = example.createCriteria();

        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(marketAdmins);
        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public MarketAdmin create(MarketAdmin admin) {
        //判断是否重名
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (admin.getUsername() != null && !"".equals(admin.getUsername())) {
            criteria.andUsernameEqualTo(admin.getUsername());
        }
        criteria.andDeletedEqualTo(false);

        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        if (!marketAdmins.isEmpty()) {
            return null;
        }
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        //插入管理员信息
        adminMapper.insert(admin);
        int id = adminMapper.selectIdByName(admin.getUsername());
        //更新管理员权限列表
        //todo
        admin.setId(id);
        return admin;
    }

    @Override
    public int delete(MarketAdmin marketAdmin) {
        marketAdmin.setDeleted(true);
        int i = adminMapper.updateByPrimaryKeySelective(marketAdmin);
        return i;
    }

    @Override
    public MarketAdmin update(MarketAdmin admin) {
        int i = adminMapper.updateByPrimaryKeySelective(admin);

        return admin;
    }

}
